package teleradiologia.repository;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import teleradiologia.domain.Technician;
import teleradiologia.utils.HibernateUtil;

public class TechnicianDaoImpl implements TechnicianDao {

	private org.springframework.security.providers.encoding.BaseDigestPasswordEncoder encoder = new Md5PasswordEncoder();

	@Override
	public String create(Technician technician) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		technician.getPerson().setPassword(
				encoder.encodePassword(technician.getPerson().getPassword(),
						technician.getPerson().getUsername()));

		String nip = (String) session.save(technician);

		tx.commit();
		session.close();

		return nip;
	}

	@Override
	public void delete(Technician technician) {
		if (technician != null) {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction tx = session.beginTransaction();

			session.delete(technician);

			tx.commit();
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Technician> getAll() {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Technician> technicians = session
				.createQuery(
						"from Technician technician order by technician.person.name asc")
				.list();

		tx.commit();
		session.close();
		return technicians;
	}

	@Override
	public Technician getByNip(String nip) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Technician technician = (Technician) session.get(Technician.class, nip);

		tx.commit();
		session.close();

		return technician;
	}

	@Override
	public void update(Technician technician) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		if (technician.getPerson().getPassword().length() < 20) {
			technician.getPerson().setPassword(
					encoder.encodePassword(
							technician.getPerson().getPassword(), technician
									.getPerson().getUsername()));
		}

		session.update(technician);

		tx.commit();
		session.close();
	}

	@Override
	public Technician getByIdPerson(long idPerson) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Technician technician = (Technician) session
				.createQuery(
						"from Technician technician where technician.person.id = :idPerson")
				.setLong("idPerson", idPerson).uniqueResult();

		tx.commit();
		session.close();

		return technician;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Technician> getAllByKrsnumber(String krsnumber) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Technician> technicians = session
				.createQuery(
						"from Technician technician where technician.radiologylab=:krsnumber order by technician.person.surname asc")
				.setString("krsnumber", krsnumber).list();

		tx.commit();
		session.close();

		return technicians;
	}
}
